// __PUBLIC__/js/page/admin/user/user
require(['../../common'], function() {
  require(['user.user'])
})

define('user.user', [
  'jquery',
  'page/grid'
], function($, Grid) {
  'use strict';

  var $table = $('.table')
  var mygrid = new Grid($table)
  mygrid.init()

  $(document)
    .on('click', '.A_del', function(e) {
      e.preventDefault()
      delUser(getASelected(this))
    })
    .on('click', '.A_recover', function(e) {
      e.preventDefault()
      recUser(getASelected(this))
    })
    .on('click', '#J_transfer', function(e) {
      e.preventDefault()
      transferUser(getSelected())
    })
    .on('click', '#J_del', function(e) {
      e.preventDefault()
      delUser(getSelected())
    })
      .on('click', '#J_delete', function(e) {
      e.preventDefault()
      deleteUser(getSelected())
    })
     .on('click', '#J_restore', function(e) {
      e.preventDefault()
      recUser(getSelected())
    })

  function getSelected() {
    var selected = mygrid.getSelected()
    if (!selected.count) {
      alert(lang.pleaseSelectAData)
      return
    }
    return selected
  }

  function getASelected(t) {
    var current = mygrid.getCurrent(t)
    var selected = {
      id: [current.id],
      index: [current.index]
    }
    return selected
  }

  function deleteUser(selected) {
    $.post(APP + '?app=admin&m=User&a=doDeleteUser', {
      ids: selected.id.join()
    }).done(function(res) {
      if (res === 0) {
        alert(lang.delFails)
      } else {
        $('#sysdeleteuser').html(res)
        alert(lang.delSuccess)
        mygrid.del(selected.index)
      }
    })
  }
  
  function delUser(selected) {
	    $.post(APP + '?app=admin&m=User&a=doDelUser', {
	      ids: selected.id.join()
	    }).done(function(res) {
	      if (res == 0) {
	        alert(lang.delFails)
	      } else {
	        alert(lang.delSuccess)
	        mygrid.del(selected.index)
	      }
	    })
	  }
  
  function recUser(selected) {
	    $.post(APP + '?app=admin&m=User&a=doRecUser', {
	      ids: selected.id.join()
	    }).done(function(res) {
	      if (res === 0) {
	        alert("恢复失败！！")
	      } else {
	        alert("恢复成功！！")
	        mygrid.del(selected.index)
	      }
	    })
	  }
  
  
  
  function transferUser(selected) {
    window.location.href = APP + '?app=admin&m=User&a=shiftUserGroup' + '&shiftIds=' + selected.id.join()
  }

  $('#J_more').on('click', function(e) {
    e.preventDefault()
    $(this).next('.more-constraints').toggle()
  })

  $('#J_search').on('click', function(e) {
    e.preventDefault()
    searchData()
  })

  function searchData() {
    var uid = $('#uid').val();
    var recycle = $('#recycle').val()
    var email = $('#email').val();
    var nickname = $('#nickname').val();
    var is_active = $('#is_active').val();
    var ugroup = $('#ugroup').val();
    var par_get = '&uid=' + uid + '&email=' + email + '&recycle=' + recycle + '&nickname=' + nickname + '&ugroup=' + ugroup + '&is_active=' + is_active;
    location.href = APP + '?app=admin&m=User&a=user' + par_get;
  }
})
